{print}    = require 'sys'
{exec}     = require 'child_process'
{spawn}    = require 'child_process'
fs         = require 'fs'
path_utils = require 'path'


#==========================================================
# Utilities
#==========================================================

# Copy one file
copy = (from, to) ->
  fs.readFile from, (err, data) ->
    throw err if err
    fs.writeFile to, data


# Copy multiple files
copyAll = (from, to) ->
  if (!path_utils.existsSync(from))
    throw "DirectoryNotFound"

  fs.readdir from, (err, files) ->
    throw err if err

    for f in files
      stats = fs.statSync("#{from}/#{f}")
      if (stats.isDirectory())
        createDirectory("#{to}/#{f}")
        copyAll("#{from}/#{f}", "#{to}/#{f}")
      else
        copy("#{from}/#{f}", "#{to}/#{f}")


# Remove a directory (recursive)
removeDirectory = (path) ->
  

# Create a directory if it doesn't exists (recursive)
createDirectory = (path) -> 
  sub_paths = path.split('/')

  path_to_create = ""

  for p in sub_paths
    path_to_create += "#{p}/"

    if (!path_utils.existsSync(path_to_create))
      fs.mkdirSync(path_to_create, '711')


# Get a list of files paths from a directory (recursive)
# Usage: getFilesSync('path/to/folder', 'coffee$', '^interface')
getFilesSync = (from, includes, excludes, results) ->
  if (!path_utils.existsSync(from))
    throw "DirectoryNotFound"
  
  results ?= new Array
  
  files = fs.readdirSync(from)
  
  for name in files
    path = "#{from}/#{name}"
    stats = fs.statSync(path)
    results.push(path) if stats.isFile() and name.match(includes)? and !name.match(excludes)?
    getFilesSync(path, includes, excludes, results) if stats.isDirectory()
  
  return results


# Merge multiple files from a directory (recursive)
merge = (from, to) ->
  files = getFilesSync(from, 'coffee$', '^interface')

  #for f in files
  #  print "#{f}\n"

  datas = new Array()
  
  for file in files
    datas.push(fs.readFileSync(file))
  
  fs.writeFileSync(to, datas.join('\n\n'))


# Merge one file into another file
mergeOne = (from, into) ->
  datas = [fs.readFileSync(into), fs.readFileSync(from)]
  fs.writeFileSync(into, datas.join('\n\n'))


# Compile
compile = (from, to, name) ->
  path = "#{to}/#{name}.coffee"
  done = false

  # build & delete the merged file
  exec "coffee -c -o #{to} #{path}", (err) ->
    if (err)
      done = true
      throw err

    fs.unlinkSync(path)
    done = true

  #wait for async method to terminate
  #wait() until done


# Merge and compile files from a directory (recursive)
# Usage: mergeAndCompile('path/from', 'path/to', 'myscript')
mergeAndCompile = (from, to, name) ->
  path = "#{to}/#{name}.coffee"

  merge(from, path)

  # build & delete the merged file
  exec "coffee -c -o #{to} #{path}", (err) ->
    throw err if err
    fs.unlink path


#==========================================================
# Tasks
#==========================================================

# Task: build
task 'build', 'Build project', ->
  removeDirectory('bin')
  createDirectory('bin/js')

  copyAll('config', 'bin')
  copyAll('assets', 'bin')

  exec 'coffee -c -o bin/js src', (err) ->
    throw err if err

  exec 'sass assets/stylesheets/quesse.scss > bin/quesse.css', (err) ->
    throw err if err

